package jalus.blog;

import jalus.Action;
import jalus.model.Comment;
import jalus.service.CommentService;
import jalus.utils.HttpUtils;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.google.appengine.api.datastore.Link;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class CommentServlet extends Action{
	
	// 删除comment,	新增comment
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)	throws IOException {
		String action = req.getParameter("action");
		if(StringUtils.equalsIgnoreCase("delete", action)){
			deleteCommnet(req, resp);
		}else if(StringUtils.equalsIgnoreCase("new", action)){
			addComment(req, resp);
		}else{
			error();
		}
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doGet(req, resp);
	}
	
	/**
	 * 新增评论
	 * @param req
	 * @param resp
	 * @throws IOException 
	 */
	private void addComment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		User user = null;
		if(userService.isUserLoggedIn()){
			user = userService.getCurrentUser();
		}
		String postidStr = req.getParameter("postid");
		// 如果匿名评论，比较验证码
		if(!userService.isUserLoggedIn()){
			String code = req.getParameter("code");
			// 对付机器人。如果是用户行为，这个不可能为null
			if(code == null){
				resp.setStatus(404);
				return;
			}
			HttpSession session = req.getSession();
			if(!code.equals(session.getAttribute("code"))){
				//验证码错误
				req.getSession().setAttribute("errmsg", "输入的验证码不正确");
				resp.sendRedirect("/blog/post?postid=" + postidStr);
				return;
			}
		}
		
		String content = req.getParameter("content");
		content = HttpUtils.htmltotxt(content);
		content = HttpUtils.ubbtohtml(content);
		Long postid = new Long(postidStr);
		String username = req.getParameter("username");
		String email = req.getParameter("email");
		String homepage = req.getParameter("homepage");
		String ip = req.getRemoteAddr(); 
		Comment comment = new Comment(postid, new Date(), username, new Text(content), ip);
		if(StringUtils.isNotEmpty(email)){
			comment.setEmail(email);
		}
		if(homepage!=null && !homepage.isEmpty()){
			if(!homepage.startsWith("http://") && !homepage.startsWith("https://")){
				homepage = "http://" + homepage;
			}
			comment.setHomepage(new Link(homepage));
		}
		if(userService.isUserLoggedIn()){
			comment.setAuthor(user);
		}
		
		CommentService commentService = CommentService.getInstance();
		commentService.addComment(comment);
		resp.sendRedirect("/blog/post?action=view&postid=" + postid);
		
	}

	/**
	 * 删除一篇文章
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	private void deleteCommnet(HttpServletRequest req, HttpServletResponse resp)throws IOException{
		UserService userService = UserServiceFactory.getUserService();
		if (!userService.isUserLoggedIn() || !userService.isUserLoggedIn()) {
			error();
			return;
		}
		
		long id = NumberUtils.toLong(req.getParameter("commentid"));
		if(id == 0){
			error();
		}
		CommentService commentService = CommentService.getInstance();
		Long postid = commentService.deleteCommentById(id);
		resp.sendRedirect("/blog/post?action=view&postid=" + postid);
	}
}
